package com.wenan.ui.controls.flashMaps
{
	import com.wenan.AppGlobal;
	import com.wenan.data.City;
	import com.wenan.data.CityCollection;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.messaging.messages.ErrorMessage;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class WenAnMapSprite extends MapSprite
	{
		private var mapArea:MapArea;
		
		public function WenAnMapSprite()
		{
			super();
			cityArray=new CityCollection(this);
			provinceArray=new ArrayCollection();
			addCityArray(cityArray);
		}

		protected override function initilizeComponents():void
		{
			mapArea = new MapArea();
			mapArea.x =-10;
			mapArea.y =0;
			addChild(mapArea);

			stopAll(mapArea);
		}
		
		private function addCityArray(array:CityCollection):void
		{
			AppGlobal.dataLayer.getMapInfo(resultHandler,faultHandler);
			
			function resultHandler(event:ResultEvent):void
			{
				var data:ArrayCollection =new ArrayCollection( event.result as Array);
				provinceArray=data; //省份数组
				for(var i:int=0;i<data.length;i++)
				{
					var cityList:Array=data.getItemAt(i).city as Array;
					for(var j:int=0;j<cityList.length;j++)
					{
						var city:City=new City();
						var obj:Object=cityList[j];
						city.id=obj["cityid"];
						city.name=obj["cityname"];
						city.x=Number(obj["X"]);
						city.y=Number(obj["Y"]);
						city.org=obj["Org"];
						city.addEventListener(MapEvent.CLICK_CITY, clickCityHandler);
						array.add(city);
					}
				}
			}
			
			function faultHandler(event:FaultEvent):void
			{
				var error:ErrorMessage = event.message as ErrorMessage;
				
				Alert.yesLabel = "确定"; 
				Alert.okLabel = "确定";
				
//				Alert.show("数据获取失败。"+error.faultCode + ": " + error.faultDetail);
			}
		}
		
		private function clickCityHandler(event:MapEvent):void
		{
			dispatchEvent(new MapEvent(MapEvent.CLICK_CITY,event.result));
		}
		
		protected override function getProvinceMovieClips():Array
		{
			var mcs:Array = [];
			
			var me:DisplayObject;
			var map:DisplayObjectContainer = mapArea;
			for(var i:uint = 0; i<map.numChildren; i++) 
			{
				me = map.getChildAt(i);
				if(me is MovieClip && me.name != "bg") 
				{
					mcs.push(me); 
				}
			}
			
			return mcs;
		}
		
		protected override function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			width = mapArea.width;
			height = mapArea.height;
		}
	}
}